package ai.people.platform.system.grpc;

import ai.people.core.grpc.lib.dict.DictRequest;
import ai.people.core.grpc.lib.dict.DictResponse;
import ai.people.core.grpc.lib.dict.DictResponseData;
import ai.people.core.grpc.lib.dict.DictServiceGrpc;
import ai.people.core.grpc.utils.StreamObserverDelegate;
import ai.people.netmon.framework.domain.system.entity.SystemDictData;
import ai.people.platform.system.service.SysDictService;
import com.google.common.base.Strings;
import io.grpc.stub.StreamObserver;
import lombok.RequiredArgsConstructor;
import net.devh.boot.grpc.server.service.GrpcService;

import java.util.List;
import java.util.Optional;

/**
 * dict grpc服务
 *
 * @author yuanqinglong
 * @date 2022/8/8 18:54
 */
@GrpcService
@RequiredArgsConstructor
public class DictGrpcService extends DictServiceGrpc.DictServiceImplBase {


    private final SysDictService sysDictServiceImpl;

    /**
     * dict类型列表
     * <pre>
     * *
     * 通过类型查询字典列表
     * &#64;mock wallet_type
     * &#64;param dictType dict类型(&lt;br&gt;wallet_type：钱包类型、&lt;br&gt;archives_social：社交媒体、&lt;br&gt;source_list：信源列表、&lt;br&gt;validity：有效性、&lt;br&gt;importance_deg：重要度
     *                 、&lt;br&gt;collection_status：采集状态、&lt;br&gt;collection_frequency：采集频率、&lt;br&gt;business_type：业务类型、&lt;br&gt;site_type：站点类型、&lt;br&gt;es_index：检索索引
     *                 &lt;br&gt;work_order_state: 工单状态、
     *                 &lt;br&gt;degree_of_danger: 危险程度、
     *                 &lt;br&gt;exp_list: 扩线点关系点
     *                 &lt;br&gt;exp_type: 扩线点类型、
     *                 &lt;br&gt;warning_message_emotion: 情感标签、
     *                 )
     * &#64;return {&#64;link Result}&lt;{&#64;link ListResponse}&lt;{&#64;link SystemDictData}&gt;&gt;
     * </pre>
     *
     * @param request          请求
     * @param responseObserver 响应观察者
     */
    @Override
    public void dictList(DictRequest request, StreamObserver<DictResponse> responseObserver) {
        StreamObserverDelegate.build(responseObserver).execute(() -> {
            List<SystemDictData> systemDictData = sysDictServiceImpl.listDictByCode(request.getDictType());
            DictResponse.Builder builder = DictResponse.newBuilder();
            for (SystemDictData systemDictDatum : systemDictData) {
                builder.addList(DictResponseData.newBuilder().setDictDataValue(Strings.nullToEmpty(systemDictDatum.getDictDataValue()))
                        .setDictCode(Strings.nullToEmpty(systemDictDatum.getDictCode()))
                        .setDictDataName(Strings.nullToEmpty(systemDictDatum.getDictDataName()))
                        .setSortSeq(Optional.ofNullable(systemDictDatum.getSortSeq()).orElse(0)).build());
            }
            return builder.build();
        });
    }
}
